﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using Dropthings.Util;
using Dropthings.Model;
using Dropthings.Web.Util;
using System.Web.Security;
using Dropthings.Business.Facade;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void RegisterButton_Click(object sender, EventArgs e)
    {
        try
        {
            if (Password.Text != ConfirmPassword.Text)
            {
                InvalidLoginLabel.Visible = true;
                InvalidLoginLabel.Text = "两次输入密码不一致.";
                return;
            }
            bool activationRequired = ConstantHelper.ActivationRequired;

            RegisterUserResponse registerUserResponse = null;
            var facade = Services.Get<Facade>();
            {
                registerUserResponse = facade.RegisterUser(Email.Text, Password.Text, Email.Text, activationRequired);
                //try
                //{
                //    facade.RegisterUser(Email.Text, Password.Text, Email.Text, activationRequired);
                //}
                //catch (Exception)
                //{
                    
                //}
                //Response.Redirect("~/Default.aspx",true);
                //return;
            }

            if (registerUserResponse != null)
            {
                if (activationRequired)
                {
                    //MailManager.SendSignupMail(Email.Text.Trim(), Password.Text.Trim(), activationRequired, registerUserResponse.UnlockKey);
                    //CookieHelper.Clear();
                    Response.Redirect("~/Confirm.aspx");
                }
                else
                {
                    FormsAuthentication.RedirectFromLoginPage(Email.Text, true);
                    //MailManager.SendSignupMail(Email.Text.Trim(), Password.Text.Trim(), activationRequired, string.Empty);
                    Response.Redirect("~/Default.aspx");
                }
            }
            else
            {
                InvalidLoginLabel.Visible = true;
            }
        }
        catch (Exception x)
        {
            if (Membership.ValidateUser(Email.Text, Password.Text))
            {
                var profile = Profile.GetProfile(Email.Text);
                profile.IsFirstVisitAfterLogin = true;
                profile.Save();
                FormsAuthentication.RedirectFromLoginPage(Email.Text, true);
                return;
            }
            Debug.WriteLine(x);
            InvalidLoginLabel.Visible = true;
            InvalidLoginLabel.Text = x.Message;
        }
    }
}